package com.hxzy.servlet;

import com.alibaba.fastjson.JSONObject;
import com.hxzy.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author nick
 */
@WebServlet("/api/search")
public class SearchServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("==========进入search servlet=============");
        //设置响应头
        resp.setContentType("application/json;charset=utf-8");

        //得到一个响应成功的对象
        Result success = Result.success();
        Object names[] = {"张三","李四","王五","赵六"};
        success.setData(names);

        //将Result的对象转换为json
        String jsonString = JSONObject.toJSONString(success);

        //将json字符串写回给客户端
        PrintWriter writer = resp.getWriter();
        writer.print(jsonString);
        writer.flush();
        writer.close();
    }
}
